import { get } from 'lodash-es'
import router from '@/router'

export function getQueryParam(param: string | string[], defaultVal = '') {
  const query = router.currentRoute.value?.query ?? {}
  const val = get(query, param) ?? defaultVal
  // 处理双重编码的情况（如 %252F -> %2F -> /）
  try {
    let decoded = decodeURIComponent(val)
    // 如果解码后仍然包含编码字符，再次解码
    if (decoded.includes('%')) {
      decoded = decodeURIComponent(decoded)
    }
    return decoded
  } catch (error) {
    // 如果解码失败，返回原始值
    return val
  }
}
